import 'package:get/get.dart';
import 'package:honeypotflutter/modules/home/api/home_api.dart';
import 'package:honeypotflutter/modules/home/help/home_modules_data.dart';
import 'package:honeypotflutter/publicBussine/express/bussine_express_logic.dart';
import 'package:honeypotflutter/publicBussine/loginInfo/bussine_userInfo.dart';
import 'package:honeypotflutter/public/public_total.dart';
import 'package:honeypotflutter/router/bussine_router_url.dart';
import '../../model/home_modulesModel.dart';

class HomeLogic extends GetxController {
  /// 投递提示
  RxString deliverRemind = RxString("待取件: 0  超时件:0");

  /// 揽件提示
  RxString receiverRemind = RxString("待揽件: 0  退回件:0");

  /// 模块数据
  RxList<HomeModulesModel> modulesList = RxList();

  final _userLogic = Get.find<UserInfoLogic>();

  final _express = Get.find<ExpressLogic>();

  /*
  * 初始化
  * */
  @override
  void onInit() {
    super.onInit();
    modulesList.addAll(HomeModulesData.modulesList);
    request();
    requestOnce();
  }

  /*
  * 请求数据
  * */
  request() {
    /// 投递信息
    HomeApi.requestDeliver().then((value) {
      if (value == null) {
        return;
      }
      deliverRemind.value = "待取件: ${value["pickedUpCount"]}  超时件: ${value["timeoutCount"]}";
    });

    /// 揽件信息
    HomeApi.requestReceive().then((value) {
      if (value == null) {
        return;
      }
      receiverRemind.value = "待揽件: ${value["waitTakeCount"]}  退回件: ${value["refuseCount"]}";
    });

    /// 网络请求
    _userLogic.requestUserInfo();
  }

  /*
  * 请求一次
  * */
  requestOnce() {
    HomeApi.requestMenuList().then((value) {
      modulesList.clear();
      modulesList.addAll(value);
    });
  }

  /*
  * 跳转投递
  * */
  pushToDeliver() {
    Get.toNamed(MGRouterUrl.deliver_scanf);
  }

  /*
   * 跳转揽件
   * */
  pushToReceiver() async {
    Get.toNamed(MGRouterUrl.gather);
  }

  /*
  *  查询数据
  * */
  HomeModulesModel findModulesInfo(int index) {
    HomeModulesModel info = modulesList[index];
    if (info.image == null || info.image!.isEmpty) {
      HomeModulesModel localModel = HomeModulesData.findOneModules(info.id);
      info.image = localModel.image;
      info.url = localModel.url;
    }
    return info;
  }

  /*
  *  选中item
  * */
  selectGridViewItem(HomeModulesModel model) {
    if (model.url == null || model.url!.isEmpty) {
      HLTotal.total("功能暂未开放");
      return;
    }
    Get.toNamed(model.url!);
  }
}
